﻿Public Class CMAZDALabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CMAZDALabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-M") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CMAZDALabelState.DoExecute")
        Dim labelType As Integer = CCommon.LABEL_MAZDA
        Dim readText As String = task.ProcessText
        Dim mazdaItemCd As String = CCommon.getMazdaItemCd(readText)
        Dim sysItemCd As String = ""
        Dim orderCd As String = CCommon.trimMazdaLabelOrderCd(CCommon.getMazdaOrderCd(readText))
        'Debug.WriteLine("orderCd=========" & orderCd)
        Dim isInMazda As Boolean = True
        Dim uniqueCd As String = readText

        sysItemCd = pdaTask.getMazdaItemCd(mazdaItemCd)
        'Debug.WriteLine("sysItemCd=========" & sysItemCd)
        If sysItemCd = "" Then
            ''エラー？

        End If

        ''読込み済みのラベルではないか確認（ユニークコード）N:アラート
        Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
        uniqueCdInfo.labelType = CCommon.LABEL_MAZDA
        uniqueCdInfo.itemCd = sysItemCd
        uniqueCdInfo.uniqueCd = uniqueCd
        uniqueCdInfo.readDateTime = Now()

        Dim result As String = ""
        Dim prevWindowMode As EnumWindowMode
        If Not pdaTask.addUniqueCd(uniqueCdInfo) Then
            Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
            Dim isMazdaAlertCheck As Boolean = AppManager.GetAppSettings.IsMazdaAlertCheck

            If isMazdaAlertCheck Then
                ''アラート
                Do While result <> "[YES]" And result <> "[NO]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_017, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.YESNO_MODE)
                        'Debug.WriteLine(CCommon.CNS_MSG_017)

                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
            Else
                result = "[YES]"
            End If
        End If
        If result = "" Or result = "[YES]" Then
            'If result = "[YES]" Then
            '    ''マツダの強制ADD
            '    ''20080323ユニークコードのロールバック機能が　最終登録を削除　から　CommitフラグがFalseの物を削除　に変わったため
            '    pdaTask.addMazdaUniqueCd(uniqueCdInfo)
            'End If

            Dim isParent As Boolean = False

            If pdaTask.checkParentLabel(labelType) Then
                If pdaTask.chkOutQty() Then
                    ''入り数チェック
                    ''正常
                    isParent = True
                    If pdaTask.commitLabel() Then
                        ''正常
                    Else
                        ''error(通常ありえない）
                        pdaTask.addWindowLabelBox(labelType, 2)
                        pdaTask.rollbackLabel()
                        'エラー
                        pdaTask.Hardware.warning()
                        ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                        SyncLock pdaTask.m_SendLockObject
                            Try
                                pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                                pdaTask.C_FileCommon.SendTrxOrder()

                            Catch ex As Exception

                            End Try
                        End SyncLock

                        Dim result2 As String = ""
                        Dim prevWindowMode2 As EnumWindowMode
                        Do While result2 <> "[OK]"
                            If result2 = "" Then
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            End If
                            result2 = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode2)



                        task.ErrorCd = "ER"
                        task.ProcessText = ""
                        nextState = GetNextState(task, GetType(CErrorState))
                        Return nextState

                    End If
                Else
                    ''入り数チェック
                    ''エラー
                    ''error
                    pdaTask.addWindowLabelBox(labelType, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock


                    Dim result2 As String = ""
                    Dim prevWindowMode2 As EnumWindowMode
                    Do While result2 <> "[OK]"
                        If result2 = "" Then
                            prevWindowMode2 = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_025, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result2 = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode2)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState


                End If
            End If

            'Dim labelData As typActionLabelRecord = pdaTask.chkGroupCd(pdaTask.C_FileCommon.GetLabel(readText))
            Dim groupCd As String = ""
            Dim inBoxQty As Integer = pdaTask.C_SHIPORDER.getInBoxQty(orderCd, sysItemCd)
            Dim isFullBox As Boolean = True
            Dim LabelRnk As String

            Dim label As New CLabelData()

            'If labelData.GroupCd Is Nothing Or labelData.GroupCd = "" Then
            'Else
            '    groupCd = labelData.GroupCd
            '    inBoxQty = labelData.ActionQty
            '    If labelData.LabelState = "F" Then
            '        isFullBox = True
            '    Else
            '        ''端数
            '        isFullBox = False
            '    End If
            'End If
            If isParent Then
                LabelRnk = "P"
            Else
                LabelRnk = "C"
            End If

            If orderCd = "" Then
                If Not isParent Then
                    orderCd = pdaTask.labelList.OrderCd
                End If
            End If

            label.Id = 0
            label.GroupCd = groupCd
            label.OrderCd = orderCd
            label.SysItemCd = sysItemCd
            label.InBoxQty = inBoxQty
            label.IsFullBox = isFullBox
            label.ReadText = readText
            label.LabelType = labelType
            label.LabelRnk = LabelRnk

            label.IsReadMazda = True
            'If readText.EndsWith(CCommon.CNS_DELIMITER_LABEL_FRACTION) Then
            '    label.IsFullBox = False
            '    isFullBox = False
            'Else
            '    label.IsFullBox = True
            '    isFullBox = True
            'End If

            Dim ret As COrderData = pdaTask.C_SHIPORDER.ChkLabelData(label)
            If Not ret Is Nothing Then
                pdaTask.setPrevData(ret)
                If CCommon.getIntLabelType(ret.OrderType) = CCommon.LABEL_MAZDA Then
                    ret.OrderRecord.RowColor = CCommon.COLOR_GREEN
                End If

                If isParent Then
                    ret.OrderRecord.ActionBoxQty += 1
                    If ret.OrderRecord.ActionOrderQty + inBoxQty <= ret.OrderRecord.OrderQty Then
                        ret.OrderRecord.ActionOrderQty += inBoxQty
                    Else
                        ret.OrderRecord.ActionOrderQty = ret.OrderRecord.OrderQty
                    End If
                    Dim idx As Integer = pdaTask.WindowManager.GetIndex(ret.OrderRecord)
                    pdaTask.WindowManager.SetCurrentIndex(idx)
                End If
                Dim labelState As CPDATask.strctNowLabelState = Nothing
                labelState.LabelType = labelType
                labelState.IsInMazda = isInMazda ''DNCラベル読込時、Mの場合はTrue
                labelState.GroupCd = groupCd
                labelState.SysItemCd = sysItemCd
                labelState.OrderCd = orderCd
                labelState.InBoxQty = inBoxQty
                labelState.IsFullBox = isFullBox
                labelState.ReadText = readText
                labelState.LabelRnk = LabelRnk
                labelState.Begin = True

                If pdaTask.addLabel(labelState, isParent) Then
                    ''正常
                    pdaTask.addGroupCd(groupCd)
                    If isParent And ret.OrderRecord.BoxQty = ret.OrderRecord.ActionBoxQty Then
                        pdaTask.shipWriteWindow2(ret.ReportCd)
                    End If

                    ''ここのコメントアウトをはずせば読んだ瞬間に非表示になる
                    Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
                    Dim isBoxEndChk As Boolean = AppManager.GetAppSettings.IsBoxEndChk
                    Dim isInnerLabelCheck As Integer = AppManager.GetAppSettings.IsInnerLabelCheck
                    If isBoxEndChk And isInnerLabelCheck = 0 And ret.OrderRecord.BoxQty = ret.OrderRecord.ActionBoxQty Then
                        pdaTask.Hardware.nextD()

                        Dim isListDeleteChk As Boolean = AppManager.GetAppSettings.IsListDeleteChk
                        If isListDeleteChk Then
                            pdaTask.WindowManager.ClearActionLabel()
                            ret.OrderRecord.Visible = False
                        End If
                    Else
                        Dim isMazdaAlarmChk As Boolean = AppManager.GetAppSettings.IsMazdaAlarmChk
                        If isMazdaAlarmChk Then
                            pdaTask.Hardware.nextE()
                        End If
                    End If

                Else
                    ''error
                    pdaTask.addWindowLabelBox(labelType, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, ret.ReportCd, ret.RowNum, ret.OrderCd, ret.SysItemCd, ret.OrderQty))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock

                    Dim result2 As String = ""
                    Dim prevWindowMode2 As EnumWindowMode
                    Do While result2 <> "[OK]"
                        If result2 = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result2 = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode2)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState

                End If

            Else
                ''error
                pdaTask.addWindowLabelBox(labelType, 2)
                pdaTask.rollbackLabel()
                'エラー
                pdaTask.Hardware.warning()
                ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                SyncLock pdaTask.m_SendLockObject
                    Try
                        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                        pdaTask.C_FileCommon.SendTrxOrder()

                    Catch ex As Exception

                    End Try
                End SyncLock

                Dim result2 As String = ""
                Dim prevWindowMode2 As EnumWindowMode
                Do While result2 <> "[OK]"
                    If result2 = "" Then
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result2 = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode2)



                task.ErrorCd = "ER"
                task.ProcessText = ""
                nextState = GetNextState(task, GetType(CErrorState))
                Return nextState

            End If

            'Dim label As New CLabelData()
            'label.Id = 0
            'label.GroupCd = pdaTask.setGroupCd(pdaTask.C_FileCommon.GetLabel(readText))
            'label.OrderCd = orderCd
            'label.SysItemCd = sysItemCd
            'label.InBoxQty = pdaTask.C_SHIPORDER.getInBoxQty(orderCd, sysItemCd)
            'If readText.EndsWith(CCommon.CNS_DELIMITER_LABEL_FRACTION) Then
            '    label.IsFullBox = False
            'Else
            '    label.IsFullBox = True
            'End If
            'Dim ret As COrderData = pdaTask.C_SHIPORDER.AddLabelData(label)
            'If Not ret Is Nothing Then
            '    Dim idx As Integer = pdaTask.WindowManager.GetIndex(ret.RowNum)
            '    pdaTask.C_SHIPORDER.WriteWindow()
            '    pdaTask.WindowManager.SetCurrentIndex(idx)
            '    If ret.OrderRecord.Visible Then
            '        pdaTask.shipWriteWindow(ret.ReportCd, ret.RowNum, 0, label)
            '    Else
            '        pdaTask.shipWriteWindow(ret.ReportCd, ret.RowNum, 1, label)
            '    End If
            'Else
            '    'エラー
            '    pdaTask.Hardware.warning()
            '    If result = "" Then
            '        pdaTask.delLastUniqueCd() ''登録したユニークコードを削除
            '    End If

            '    pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData("ER"))
            '    pdaTask.C_FileCommon.SendTrxOrder()

            '    Dim result2 As String = ""
            '    Do While result2 <> "[OK]"
            '        If result2 = "" Then
            '            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
            '            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
            '            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
            '        End If
            '        result2 = task.ReadTextWait(-1)
            '        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
            '    Loop
            '    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)


            '    task.ErrorCd = "ER"
            '    task.ProcessText = ""
            '    nextState = GetNextState(task, GetType(CErrorState))
            '    Return nextState
            'End If
        End If

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CMAZDALabelState.DoExit")
        Return True
    End Function


End Class
